<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>教练</title>
    <link rel="stylesheet" href="../../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../../css/okadmin.css">

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend style="margin-left: 10%">我的学员</legend>
</fieldset>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row layui-col-space15 layui-tab layui-tab-brief" style="width: 60%;margin: 0 auto">
                <ul class="layui-tab-title">
                    <li class="layui-this">当前学员</li>
                    <li>历史学员</li>
                </ul>
                <div class="layui-tab-content" style="height: 100px;">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-timeline" id="studentList" lay-filter="studentList">
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div class="layui-timeline" id="historyChoices" lay-filter="historyChoices">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="modify" >修改</a>
    <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="studyTypeChange" >学习完毕</a>
</script>

<script type="text/html" id="endBarDemo">
    <a class="layui-btn layui-btn-xs" lay-event="modify" >修改</a>
</script>
<script src="../../lib/layui/layui.js"></script>
<script src="../../js/jquery.min.js"></script>
<script src="../../js/application.js"></script>
<script>
    layui.use(['table', 'layer', 'rate', 'element','okLayer'], function () {
        var table = layui.table,//当前学员
            $ = layui.$,
            form = layui.form,
            layer = layui.layer,
            element = layui.element, //Tab的切换功能，切换事件监听等，需要依赖element模块
            rate = layui.rate,
            table2=layui.table,//历史学员
            okLayer=layui.okLayer

        if(isEmpty(user['coachId'])){
            layer.msg('账号信息异常,请重新登陆', {
                icon: 1,
                time: 1000
            }, function () {
                window.sessionStorage.setItem("user", "");
                window.location = mainUri+"/login.html";
            });
            return
        }
        var setRate = function () {
            //多个评分
            layui.each($('.lf_rate'), function (index, elem) {

                var configTemp = $(elem).attr('lay-data');
                try {
                    configTemp = eval('(' + configTemp + ')');

                } catch (e) {
                    configTemp = {};

                }

                rate.render($.extend(true, {
                    elem: elem
                }, configTemp));

            });
        }

        //当前学员
        var studentTable=table.render({
            elem: '#studentList',
            where:{coachId:user['coachId'],studyType:0},
            url: mainUri+"/vChoice/selects", //数据接口
            cols: [[ //表头
                {field: 'studentId', title: 'ID'},
                {field: 'fullname', title: '姓名' },
                {field: 'agender', title: '性别',templet:function(d){
                        if(d.agender==true){
                            return "男"
                        }else if(d.agender==false){
                            return "女"
                        }
                    } },
                {field: 'studyStatus', title: '当前学习阶段',templet:function(d){
                        if(d.studyStatus==1){
                            return "科目一"
                        }else if(d.studyStatus==2){
                            return "科目二"
                        }else if(d.studyStatus==3){
                            return "科目三"
                        }else if(d.studyStatus==4){
                            return "科目四"
                        }
                    } },
                {field: 'phone', title: '电话'},
                {field: 'email', title: '邮箱'},
                {fixed: 'right', title:'操作', toolbar: '#barDemo',width:185}
            ]],
        });

        //历史学员
        var historyChoicesTable=table2.render({
            elem: '#historyChoices',
            where:{coachId:user['coachId'],studyType:1},
            url: mainUri+"/vChoice/selects", //数据接口
            cols: [[ //表头
                {field: 'studentId', title: 'ID'},
                {field: 'fullname', title: '姓名' },
                {field: 'agender', title: '性别',templet:function(d){
                        if(d.agender==true){
                            return "男"
                        }else if(d.agender==false){
                            return "女"
                        }
                    } },
                {field: 'phone', title: '电话'},
                {field: 'email', title: '邮箱'},
                {fixed: 'right', title:'操作', toolbar: '#endBarDemo',width:100}
            ]],
            page: {
                layout: ['prev', 'page', 'next', 'skip', 'count'],
                groups: 3,
                limit: 15,
                theme: '#1E9FFF',
            },
        });

        function editStudent(data,type) {
            let uri="studentEditByCoach.html?"+dataToString(data)
            okLayer.open("信息修改", uri, "40%", "50%", function (layero) {
                let iframeWin = window[layero.find("iframe")[0]["name"]];
            }, function () {
                if(type==0) {
                    studentTable.reload({
                        where: {coachId: user['coachId'], studyType: 0}
                    });
                }else if(type==1){
                    historyChoicesTable.reload({
                        where:{coachId:user['coachId'],studyType:1},
                    })
                }
            })
        }
        function typeChange(d){
            if(isEmpty(d)||isEmpty(d['studentId'])||isEmpty(d['choiceId'])){
                layer.msg("当前选择的学员信息异常,亲刷新重试")
                return
            }
            layer.confirm('该学员已经学习完毕?', function (index) {
                var studentIds=[];
                var choiceIds=[];
                studentIds.push(d['studentId'])
                choiceIds.push(d['choiceId'])
                var choices={studentIds:studentIds,choiceIds:choiceIds};
                if(confirm("确定该学员已经学习完毕?")) {
                    myAjax(mainUri+"/choice/updates",{choicesStr:JSON.stringify(choices),studyType:1,coachId:user["coachId"]},function(d){
                        if(d['code']=='0'){
                            layer.msg('该学员已经学习完毕', {
                                icon: 1, time: 2000, end: function () {
                                    studentTable.reload({
                                        where: {coachId: user['coachId'], studyType: 0}
                                    });
                                    layer.closeAll();
                                }
                            })
                        }else{
                            layer.msg('结束该学员的当前学习阶段'+" <span style='color:red'>失败</span>")
                        }
                    })
                }
            });
        }

        //监听行工具事件
        table.on('tool(studentList)', function(obj){
            var data = obj.data;
            if(obj.event === 'modify'){
                editStudent(data,0)
            }else if(obj.event === 'studyTypeChange'){
                typeChange(data)
            }
        });

        //监听行工具事件
        table2.on('tool(historyChoices)', function(obj){
            var data = obj.data;
            // console.log(obj)
            if(obj.event === 'modify'){
                editStudent(data,1)
            }
        });


    });
</script>
</html>
`